<template>
	<view class="p-10 fs-15  pb-120">
		<view class="flex gap-10">
			<view class="bg-white p-15 flex-1 round-8">
				<view class="bg-fa  round-8 w-p-100 h-p-100  flex flex-center overflow-hidden relative">
					<view class="flex flex-column gap-4 flex-y-center" v-if="form.url==''" @click="upload">
						<text class="iconfont icon-shangchuanwenjian fs-30 text-666"></text>
						<text class="fs-14 ">点击上传文件</text>
					</view>
					<video :src="form.url" v-else class="w-p-100 h-p-100"></video>
					<view class="bg-21 absolute top-0 left-0 fs-13 text-white px-8 py-4 round-bottom-right-8"
						v-if="form.url!=''" @click="upload">重新选择</view>
				</view>
			</view>
			<rightList></rightList>
		</view>

		<view class='bg-white p-15 round-8 mt-20'>
			<text class="fw-600">形象名称</text>
			<view class="bg-fa round-6 mt-6 p-12">
				<input placeholder="请输入形象名称..." class="fs-14" v-model="form.title" />
			</view>
		</view>

		<y-footer @success="submit">{{sbumitText}} </y-footer>
	</view>
</template>

<script setup>
	import { computed, ref } from 'vue';
	import rightList from './modules/right-list.vue';
	import { uploadVideo } from '@/api/system.js'
	import { createScene } from '@/api/scene.js'
	import { $page, $tool } from '@/utils';
	import { useRefs, useWebConfigStore } from '@/stores'
	const { WEBCONFIG } = useRefs(useWebConfigStore())
	
	const sbumitText = computed(() => {
		if (WEBCONFIG?.value.scenePoint == 0 || WEBCONFIG?.value.scenePoint == '0') {
			return '免费创建';
		}
		return '创建形象 消耗算力:' + WEBCONFIG?.value.scenePoint
	})
	
	const form = ref({
		title: "",
		url: ""
	})

	const upload = () => {
		uni.chooseVideo({
			sourceType: ['album'],
			compressed: false,
			success(res) {
				uni.showLoading({
					title: '视频上传中...'
				})
				uploadVideo({ filePath: res.tempFilePath }).then((res) => {
					uni.hideLoading()
					form.value.url = res.data.file_url
					uni.showToast({
						title: '视频上传成功'
					})
				}).catch((err) => {
					uni.hideLoading()
				})
			}
		})
	}


	const submit = () => {
		if (form.value.title == '' || form.value.url == '') {
			$tool.toast('请填写完善数据', 'error');
			return;
		}
		uni.showLoading({
			title: '任务提交中...'
		})
		createScene(form.value).then(res => {
			$tool.toast('任务提交成功');
			setTimeout(() => {
				$page.back();
			}, 500)
		})
	}
</script>

<style>

</style>